<template>
  <div class="tab-title-page goods_list">
    <el-tabs v-model="activeName" class="flex-1 nav_tabs" @tab-click="handleClick">
      <el-tab-pane :label="`已上架活动(${count.status_put})`" name="1" />
      <el-tab-pane :label="`已下架活动(${count.status_off})`" name="2" />
    </el-tabs>
    <el-divider class="middle_divider" />
    <div class="child_page">
      <div class="common_form_search">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item label="活动ID">
            <el-input v-model="searchForm.id" placeholder="请输入活动ID" clearable />
          </el-form-item>
          <el-form-item label="活动标题">
            <el-input v-model="searchForm.title" placeholder="请输入活动标题" clearable />
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="search()">查询</el-button>
            <el-button size="small" @click="reset()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-divider />
      <div style="padding:0 30px">
        <div class="flex align-center table-top-btns">
          <h5 class="flex align-center">
            <svg-icon style="margin-right:15px" icon-class="list" :size="15" />数据列表
          </h5>
          <el-button size="small" class="add-btn" icon="el-icon-plus" @click="activityAdd()">添加活动</el-button>
        </div>
        <el-table v-show="tableData.length > -1" :data="tableData" border>
          <el-table-column align="center" label="活动名称" prop="title" width="150" />
          <el-table-column align="center" prop="info" label="说明" />
          <el-table-column align="center" prop="valid" label="活动有效期/天"/>
          <el-table-column align="center" prop="create_at" label="创建时间" width="100" />
          <el-table-column align="center" prop="is_show" label="活动状态" width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.is_show===1?'上架':'下架' }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="remark" label="备注" />
          <el-table-column v-if="activeName == 2" align="center" prop="check_reason" label="原因" />
          <el-table-column fixed="right" align="center" label="操作" width="230">
            <template slot-scope="scope">
              <el-button type="warning" size="mini" @click="changeStatus(scope.row.id, scope.row.is_show)">
                {{scope.row.is_show == 1 ? '下架' : '上架' }}
              </el-button>
              <el-button size="mini" type="danger" @click="delActivity(scope.row.id)">删除</el-button>
              <el-dropdown v-if="$hasMethod('#view') || activeName == 1 || activeName == 2" @command="command => handleMore(command, scope.row.id, scope.row)">
                <span class="el-dropdown-link blue-font" style="margin-left:10px">
                  更多操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <!-- <el-dropdown-item command="1">查看</el-dropdown-item> -->
                  <el-dropdown-item command="3">编辑活动</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <custom-page ref="customPage" :total="total" @getList="getList" />
      </div>
      <update-dialog ref="updateDialog" activity-id="actId" :title="title" :update-list="updateList" @getList="getList" />
      <off-dialog ref="offDialog" @getList="getList" />
      <set-dialog ref="setDialog" @getList="getList" />
    </div>
  </div>
</template>
<script>
import myDatePicker from '@/components/myDatePicker'
import customPage from '@/components/customPage'
import updateDialog from './components/activity/updateDialog'
import setDialog from './components/setDialog'
import offDialog from './components/offDialog'
import { activityDel, activityList, activityShow } from '@/api/audit'
export default {
  components: { myDatePicker, customPage, updateDialog, setDialog, offDialog },
  data() {
    return {
      searchForm: {
        id:'',
        title:'',
        activity_status:'1'
      },
      updateList:{},
      activeName: '1',
      count: {
        status_put: 0,
        status_off: 0,
        status_lease: 0
      },
      title:'',
      total: 0,
      tableData: [],
      categoryList: [],
      goodsList: {}
    }
  },
  mounted() {
    this.getSelectList()
    this.getList()
  },
  methods: {
    // 设置活动
    setActivity(id) {
      this.$refs.setDialog.goodsId = id
      this.$refs.setDialog.dialogVisible = true
    },
    // 上架/下架
    changeStatus(id, status) {
      const params={
        id:id,
        is_show:status
      }
      if (status === 1) {
        // 上架转下架
        this.$confirm('您确定要下架商品吗？', '下架', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          activityShow({id:id,is_show:2}).then(res => {
            this.getList(true)
          })
        }).catch(() => { })
      } else {
        // 下架转上架
        this.$confirm('您确定要上架商品吗？', '上架', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          activityShow({id:id,is_show:1}).then(res => {
            this.getList(true)
          })
        }).catch(() => { })
      }
    },
    handleMore(command, id, row) {
      if (command === '1') {
        // 查看
      } else if (command === '3') {
        this.updateList=row
        // 编辑活动
        this.title='编辑活动'
        // this.$refs.updateDialog.activityId = id
        this.$refs.updateDialog.dialogVisible = true
      } 
    },
    delActivity(val){
      this.$confirm('您确定要删除吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        activityDel({ id:val }).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList(true)
        })
      }).catch(() => { })
    },
    activityAdd(){
      this.title='添加活动'
      this.$refs.updateDialog.dialogVisible = true
    },
    // 查询
    search() {
      this.$refs.customPage.page = 1
      this.getList(true)
    },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset()
      }
      this.searchForm = {
        id:'',
        title:'',
        activity_status:'1'
      }
      this.getList()
    },
    handleClick(tab) {
      this.searchForm.activity_status=tab.name
      this.activeName = tab.name
      this.$refs.customPage.page = 1
      this.getList()
    },
    // 获取数据
    getList(flag) {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage
      if (!flag) {
        this.tableData = []
      }
      activityList({
        page,
        pageSize,
        ...this.searchForm
      }).then(res => {
        this.tableData = res.data || []
        this.total = res.total
        for (const key in this.count) {
          this.count[key] = res.count[key]
        }
      })
    },
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.getList(true)
    })
  }
}
</script>
<style lang="scss" scoped>
.common_form_search {
  padding: 15px 30px 0;
}

.child_page {
  padding-bottom: 15px;
}

.goods_info {
  .info {
    margin-left: 10px;

    p {
      color: #666666;
    }

    p:first-child {
      color: #999999;
    }
  }
}

.activity {
  line-height: 24px;

  span {
    width: 20px;
    height: 20px;
    margin-left: 10px;
  }
}
</style>
